//
//  BackTableView.m
//  geiTA
//
//  Created by 曲丹阳 on 14-11-19.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BackTableView.h"
#import "FindCell.h"
#import "FindBackDetailViewController.h"

@implementation BackTableView

- (void)dealloc
{
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.backarray = [NSMutableArray array];
        self.data = [NSData data];
        
        self.delegate = self;
        self.dataSource = self;
        
        NSString *urlstr = [NSString stringWithFormat:@"http://api.liwushuo.com/v1/collections?limit=12&offset=0"];
        NSURL *url = [NSURL URLWithString:urlstr];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            self.data = data;
            [self createJson];
            [self reloadData];
        }];
        
        [self createBackTable];
        
        
    }
    
    return self;
}



#pragma mark -BackTableView
- (void)createBackTable
{
    [self reloadData];
    [self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_backarray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *pool = @"backpool";
    FindCell *cell = [tableView dequeueReusableCellWithIdentifier:pool];
    if (!cell) {
        cell = [[FindCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool];
        
    }
    FindModel *model = [_backarray objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return tableView.frame.size.height / 4.2;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    FindBackDetailViewController *backdetail = [[FindBackDetailViewController alloc] init];
    
    UINavigationController *sec = [[UINavigationController alloc] initWithRootViewController:backdetail];
    
    
    
    
}

- (void)createJson
{
    NSMutableDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
    NSMutableDictionary *data = [bigdic objectForKey:@"data"];
    NSMutableArray *collections = [data objectForKey:@"collections"];
    for (NSDictionary *smalldic in collections) {
        FindModel *model = [[FindModel alloc] init];
        [model setValuesForKeysWithDictionary:smalldic];
        [_backarray addObject:model];
    }
    [self reloadData];
}

//- (void)GetData:(NSData *)data
//{
//    self.data = data;
//    [self createJson];
//}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
